import { defineStore } from 'pinia'
import piniaPersistConfig from './piniaPersist'

interface UserState {
  phoneNumber: string | number
  openId: string | number
  token: string
  isAdmin: boolean
  userId: string | number
  avatar: string
  loginTime: string
}
export const useUserStore = defineStore({
  id: 'user',
  state: (): UserState => ({
    phoneNumber: null,
    openId: null,
    token: null,
    isAdmin: false,
    userId: null,
    avatar: null,
    loginTime: null,
  }),
  getters: {
    isLoginEd: (state) => !!state.phoneNumber && !!state.userId,
  },
  actions: {
    setUserInfo(userInfo: UserState) {
      this.phoneNumber = userInfo.phoneNumber
      this.openId = userInfo.openId
      this.token = userInfo.token
      this.isAdmin = userInfo.isAdmin
      this.userId = userInfo.userId
      this.avatar = userInfo.avatar
      this.loginTime = userInfo.loginTime
    },
  },
  persist: piniaPersistConfig('user'),
})
